package com.service.infoEntry.Rabbit;


import com.alibaba.fastjson.JSONArray;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.rabbitmq.client.Channel;
import com.service.config.utils.RedisUtil;
import com.service.infoEntry.config.MailBean;
import com.service.infoEntry.config.MailUtil;
import com.service.infoEntry.config.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.*;

/**
 * 一个生产者，一个消费者
 */
@Component
public class MsgReceiver {
    private final ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
    private final ExecutorService product = new ThreadPoolExecutor(10,20,200L, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>(),namedThreadFactory);
    //public static ExecutorService product = Executors.newFixedThreadPool(20);
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    RedisUtil redisUtil;
    @Resource
    MailUtil mailUtil;
    @RabbitListener(queues = RabbitConfig.QUEUE_A)
    public void processA(Channel channel, String content, Message message) throws Exception {
        List<MailBean> mailBeans = JSONArray.parseArray(content, MailBean.class);
        logger.info(">>>>>>>>>>>>>>>>>已获取行程单队列中消息"+ mailBeans.size());
        product.submit(()->{
            mailUtil.sendHTMList(mailBeans);
        });
    }
    @RabbitListener(queues = RabbitConfig.QUEUE_B)
    public void processB(Channel channel, String content, Message message) throws Exception {
        List<MailBean> mailBeans = JSONArray.parseArray(content, MailBean.class);
        logger.info(">>>>>>>>>>>>>>>>>已获取项目和事务队列中消息："+ mailBeans.size());
        product.submit(()->{
            mailUtil.sendHTMList(mailBeans);
        });
    }
}

